package alpaca.view.panels;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;

import net.miginfocom.swing.MigLayout;
import alpaca.core.IUser;
import alpaca.ctrl.ControllerFactory;
import alpaca.ctrl.EditUserCtrl;

public class UserEditDialog extends JDialog {

	private static final String EDIT_USER_TITLE = "Edit User";
	private final JPanel contentPanel = new JPanel();
	private JTextField lastNameTextField;
	private JTextField firstNameTtextField;
	private JTextField emailTextField;
	private JCheckBox chckbxActive;
	private JTextArea userLabel;
	private final Action action = new CancelAction();
	private final Action action_1 = new EditAction();
	private final IUser user;
	private JTextField passwordTextField;

	/**
	 * Create the dialog.
	 */
	public UserEditDialog(IUser user) {
		getContentPane().setEnabled(false);
		setTitle(EDIT_USER_TITLE);
		setIconImage(new ImageIcon("img//alpaca.png").getImage());
		setBounds(100, 100, 267, 334);
		getContentPane().setLayout(new BorderLayout(0, 0));
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel);
		contentPanel.setLayout(new MigLayout("", "[][200px,grow,fill]", "[30px][10px][][10px][][10px][][10px][][][10px]"));
		{
			JLabel lblNewLabel_3 = new JLabel("User:");
			contentPanel.add(lblNewLabel_3, "cell 0 0,alignx right");
		}
		{
			userLabel = new JTextArea("");
			userLabel.setBackground(UIManager.getColor("Label.background"));
			userLabel.setWrapStyleWord(true);
			userLabel.setLineWrap(true);
			contentPanel.add(userLabel, "cell 1 0");
		}
		{
			JLabel lblNewLabel = new JLabel("Last Name");
			contentPanel.add(lblNewLabel, "cell 0 4,alignx right");
		}
		{
			lastNameTextField = new JTextField();
			contentPanel.add(lastNameTextField, "cell 1 4,growx");
			lastNameTextField.setColumns(10);
		}
		{
			JLabel lblNewLabel_1 = new JLabel("First name");
			contentPanel.add(lblNewLabel_1, "cell 0 2,alignx right");
		}
		{
			firstNameTtextField = new JTextField();
			contentPanel.add(firstNameTtextField, "cell 1 2,growx");
			firstNameTtextField.setColumns(10);
		}
		{
			JLabel lblNewLabel_2 = new JLabel("E-mail");
			contentPanel.add(lblNewLabel_2, "cell 0 6,alignx trailing");
		}
		{
			emailTextField = new JTextField();
			contentPanel.add(emailTextField, "cell 1 6,growx");
			emailTextField.setColumns(10);
		}

		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setAction(action_1);
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setAction(action);
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		this.user = user;
		
		firstNameTtextField.setText(user.getFirstName());
		lastNameTextField.setText(user.getLastName());
		emailTextField.setText(user.getEmail());
		userLabel.setText(user.toString());
		userLabel.setFont(firstNameTtextField.getFont());
		{
			JLabel lblNewLabel_4 = new JLabel("Password");
			contentPanel.add(lblNewLabel_4, "cell 0 8,alignx trailing");
		}
		{
			passwordTextField = new JTextField();
			contentPanel.add(passwordTextField, "cell 1 8,growx");
			passwordTextField.setColumns(10);
		}
		{
			chckbxActive = new JCheckBox("Active");
			contentPanel.add(chckbxActive, "cell 1 10");
		}
		chckbxActive.setSelected(user.isActive());
		
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setVisible(true);
		setAlwaysOnTop(true);
	}

	private class CancelAction extends AbstractAction {
		public CancelAction() {
			putValue(NAME, "Cancel");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			dispose();
		}
	}
	private class EditAction extends AbstractAction {
		public EditAction() {
			putValue(NAME, "Save edit");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			String firstName = firstNameTtextField.getText();
            String lastName = lastNameTextField.getText();
            String email = emailTextField.getText();
            String password = passwordTextField.getText();
            boolean active = chckbxActive.isSelected();

            EditUserCtrl ctrl = (EditUserCtrl) ControllerFactory.getController(EditUserCtrl.class);
            ctrl.setEditedUser(user, firstName, lastName, email, password, active);
            ctrl.execute();
            dispose();
		}
	}
}
